package com.nenmei.source;

import ch.qos.logback.core.db.DBHelper;
import com.nenmei.source.bean.Pet;
import com.nenmei.source.bean.User;
import com.nenmei.source.config.MyConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

/**
 * @author yuyif
 */
@SpringBootApplication
public class Springboot2HelloworldApplication {
    public static void main(String[] args) {

        //1,返回我们IOC容器
        ConfigurableApplicationContext run = SpringApplication.run(Springboot2HelloworldApplication.class, args);

/*        //2,查看容器里面的组件
        String[] names = run.getBeanDefinitionNames();
        for (String name : names) {
            System.out.println(name);
        }

        //3,从容器中是否有获取组件
        Pet pet1 = run.getBean("tomPet", Pet.class);
        Pet pet2 = run.getBean("tomPet", Pet.class);
        System.out.println("组件: "+(pet1==pet2));  //组件: true

        //配置类MyConfig本身也是一个组件
        MyConfig bean = run.getBean(MyConfig.class);
        System.out.println(bean);  //com.nenmei.source.config.MyConfig@5b6e8f77

        // 如果@Configuration(proxyBeanMethods = true),则MyConfig这个bean是代理对象,
        // springboot会自动检查容器中是否有有没有这个方法返回,如果有,则保持组件单实例,如果没有则创建一个新的
        // 如果@Configuration(proxyBeanMethods = false),bean不是代理对象
        User user = bean.user01();
        User user1 = bean.user01();
        System.out.println(user == user1);


        User user01 = run.getBean("user01", User.class);
        Pet pet = run.getBean("tomPet", Pet.class);

        //如果@Configuration(proxyBeanMethods = true),结果为true,因为组件相互依赖了
        //如果@Configuration(proxyBeanMethods = false),结果为false,因为组件没有相互依赖,等于重新new了一个组件
        System.out.println(user01.getPet() == pet);



        //5,获取import导入得到的名字为User的组件
        String[] beanNamesForType = run.getBeanNamesForType(User.class);
        System.out.println("------------------------");
        for (String s : beanNamesForType) {
            System.out.println(s);
        } //com.nenmei.source.bean.User

        DBHelper bean1 = run.getBean(DBHelper.class);
        System.out.println(bean1); //ch.qos.logback.core.db.DBHelper@5b5caf08*/


        boolean user01 = run.containsBean("user01");
        System.out.println("容器中是否有user01组件: "+user01);

        boolean tomPet = run.containsBean("tomPet");
        System.out.println("容器中是否有tomPet组件: "+tomPet);

        boolean employee01 = run.containsBean("employee01");
        System.out.println("容器中是否有employee01组件: "+employee01);

        boolean kitty = run.containsBean("kitty");
        System.out.println("容器中是否有kitty组件: "+kitty);

        boolean king = run.containsBean("king");
        System.out.println("容器中是否有king组件: "+king);

        boolean userBai = run.containsBean("userBai");
        boolean petCat = run.containsBean("petCat");
        System.out.println("容器中是否有userHaHa组件: "+userBai);
        System.out.println("容器中是否有petHeHe组件: "+petCat);
    }

}



